package com.yimi.yinhepda.views.kua_yue_spotGoods

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.pda.ym.utils.CarNoSelectDialog
import com.yimi.yinhepda.R
import com.yimi.yinhepda.adapter.RecycleAdapter.KuaYueTaskAdapter
import com.pda.ym.base.CommonViewHolder
import com.pda.ym.base.BaseTitleActivity
import com.pda.ym.callbacks.UICallBack
import com.yimi.yinhepda.db.dao.CarPlateTableDao
import com.pda.ym.db.table.CarPlateTable
import com.yimi.yinhepda.entity.bean.KuaYueTaskEntity
import com.yimi.yinhepda.entity.response.KuaYueTaskResponse
import com.pda.ym.net.NetRequest
import com.pda.ym.utils.Constants
import com.pda.ym.utils.SoundVibratorManager
import com.yimi.yinhepda.utils.*
import com.yimi.yinhepda.views.load_module.LoadUnloadGroupActivity
import kotlinx.android.synthetic.main.activity_kua_yue_task.*
import kotlinx.android.synthetic.main.select_car_plate.*

/**
 * @author: lj
 * Date: 2020/4/18 10:04
 * Description:跨越点货界面
 */
class KuaYueTaskActivity : BaseTitleActivity(), CommonViewHolder.onItemCommonClickListener {

    //任务列表适配器
    private var mAdapter: KuaYueTaskAdapter? = null
    //跨越任务集合
    private var mDataList = mutableListOf<KuaYueTaskEntity>()
    //跨越任务实体
    private var mTaskEntity: KuaYueTaskEntity? = null

    override fun getPageName(): String {
        return "跨越任务"
    }

    override fun setContentViewId(): Int {
        return R.layout.activity_kua_yue_task
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        btn_carPlate.setOnClickListener(this)
        btn_down.setOnClickListener(this)
        btn_back.setOnClickListener(this)
        btn_refresh.setOnClickListener(this)
        btn_exec.setOnClickListener(this)

        //输入的英文转大写
        et_carNo.transformationMethod = TransInformation()

        //设置车牌
        val carPlateTables: List<CarPlateTable> = CarPlateTableDao().getAllData()
        if (!carPlateTables.isNullOrEmpty()) {
            btn_carPlate.text = carPlateTables[0].carPlate
        }
    }

    override fun onResume() {
        super.onResume()
        //默认根据当前部门拉取跨越任务
        getTaskInfo("", sharedPreferUtil.deptCode)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.btn_carPlate -> {
                //省市车牌按钮
                CarNoSelectDialog.showCarPlateDialog(this) { carPlate -> btn_carPlate.text = carPlate }
            }
            R.id.btn_down -> {
                //下拉按钮
                if ("" == et_carNo.text.toString()) {
                    showToast("请输入车牌号！")
                    return
                }
                //拉取跨越任务
                getTaskInfo(btn_carPlate.text.toString() + et_carNo.text.toString().toUpperCase(), sharedPreferUtil.deptCode)
            }
            R.id.btn_refresh -> {
                et_carNo.setText("")
                getTaskInfo("", sharedPreferUtil.deptCode)
            }
            R.id.btn_back -> finish()
            R.id.btn_exec -> {
                //执行按钮
                if (mTaskEntity == null || !mTaskEntity!!.isCheck) {
                    showToast("请选择一项任务执行！")
                    SoundVibratorManager.playSound(2)
                    return
                }
                val intent = Intent(this, LoadUnloadGroupActivity::class.java)
                intent.putExtra("scanType", 9)
                intent.putExtra(Common.KUAYUE_TASK_KEY, mTaskEntity)
                startActivity(intent)
            }
        }
    }

    /**
     * 拉取跨越任务
     */
    private fun getTaskInfo(carNo: String, deptCode: String) {
        showPgDlg("查询任务中...")
        val params = mutableMapOf<String, String>()
        if (carNo != "") {
            params["carNo"] = carNo
        }
        params["deptCode"] = deptCode
        NetRequest().setMethod(NetRequest.Method.GET)
                .setUrl(Constants.URL_QUERY_KUAYUN_TASK)
                .setParams(params)
                .setResponsClazz(KuaYueTaskResponse::class.java)
                .execute(object : UICallBack() {
                    override fun onSuccess(obj: Any?) {
                        dismissPgDlg()
                        mDataList.clear()
                        val response = obj as KuaYueTaskResponse
                        if (!response.data.isNullOrEmpty()) {
                            mDataList.addAll(response.data)
                            SoundVibratorManager.playSound(1)
                        } else {
                            showToast("未查询到任务！")
                            SoundVibratorManager.playSound(2)
                        }
                        loadUIAdapter()
                    }

                    override fun onError(msg: String?) {
                        dismissPgDlg()
                        showToast(msg)
                    }

                })
    }

    /**
     * 加载UI列表适配器
     */
    private fun loadUIAdapter() {
        if (mAdapter == null) {
            mAdapter = KuaYueTaskAdapter(this, mDataList, this)
            rv_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
            rv_list.adapter = mAdapter
        } else {
            mAdapter!!.notifyDataSetChanged()
        }
    }

    override fun onItemClickListener(position: Int) {
        mTaskEntity = mDataList[position]
        if (mTaskEntity != null) {
            if (mTaskEntity!!.isCheck) {
                mTaskEntity!!.isCheck = false
            } else {
                mDataList.forEach {
                    it.isCheck = false
                }
                mTaskEntity!!.isCheck = true
            }
            mAdapter?.notifyDataSetChanged()
        }
    }

    override fun onItemLongClickListener(position: Int) {
    }


    override fun showBack(): Boolean {
        return false
    }

    override fun setBack() {
        finish()
    }

    override fun setTitle(): String {
        return "任务拉取"
    }

}